#include "Audio.h"
#include "Sample.h"

Audio::Audio()
{
	system = NULL;
}

Audio::~Audio()
{
	for (Iterator i = samples.begin(); i != samples.end(); i++)
	{
		(*i) = NULL;
	}
	FMOD_System_Release(system);
}

bool Audio::Init()
{
	if (FMOD_System_Create(&system) != FMOD_OK)
	{
		return false;
	}
	if (FMOD_System_Init(system, 100, FMOD_INIT_NORMAL, NULL) != FMOD_OK)
	{
		return false;
	}
	return true;
}

void Audio::Update()
{
	FMOD_System_Update(system);
}

Sample* Audio::Load(string fileName)
{
	if (fileName.length() == 0)
		return false;
	Sample* sample = new Sample();
	FMOD_RESULT res;
	res = FMOD_System_CreateSound(
		system,				//Fmod system
		fileName.c_str(),	//filename
		FMOD_DEFAULT,		//default audio
		NULL,				//NULL
		&sample->sample);	//pointer to sample
	if (res != FMOD_OK)
		sample = NULL;
	return sample;
}

bool Audio::Load(string fileName, string name)
{
	if (fileName.length() == 0 || name.length() == 0)
		return false;
	Sample* sample = new Sample();
	sample->setFileName(name);
	FMOD_RESULT res;
	res = FMOD_System_CreateSound(
		system,
		fileName.c_str(),
		FMOD_DEFAULT,
		NULL,
		&sample->sample);
	if (res != FMOD_OK)
		return false;
	samples.push_back(sample);
	return true;
}

bool Audio::SampleExists(string name)
{
	for (Iterator i = samples.begin(); i != samples.end(); i++)
	{
		if ((*i)->getFileName() == name)
			return true;
	}
	return false;
}

bool Audio::IsPlaying(string name)
{
	Sample* sample = FindSample(name);
	if (sample == NULL)
		return false;
	int index;
	FMOD_Channel_GetIndex(sample->channel, &index);
	return (index > 0);
}

Sample* Audio::FindSample(string name)
{
	Sample* sample = NULL;
	for (Iterator i = samples.begin(); i != samples.end(); i++)
		if ((*i)->getFileName() == name)
		{
			sample = (*i);
			break;
		}
	return sample;
}

bool Audio::Play(string name)
{
	FMOD_RESULT res;
	Sample* sample = FindSample(name);
	if (sample->sample != NULL)
	{
		res = FMOD_System_PlaySound(
			system,
			FMOD_CHANNEL_FREE,
			sample->sample,
			true,
			&sample->channel);
		if (res != FMOD_OK)
			return false;
		FMOD_Channel_SetLoopCount(sample->channel, -1);
		FMOD_Channel_SetPaused(sample->channel, false);
	}
	return true;
}

bool Audio::PlayBg(string name)
{
	FMOD_RESULT res;
	Sample* sample = FindSample(name);
	if (sample->sample != NULL)
	{
		res = FMOD_System_PlaySound(
			system,
			FMOD_CHANNEL_FREE,
			sample->sample,
			false,
			&sample->channel);
		if (res != FMOD_OK)
			return false;
		FMOD_Channel_SetLoopCount(sample->channel, -1);
		FMOD_Channel_SetPaused(sample->channel, false);
	}
	return true;
}

bool Audio::Play(Sample* sample)
{
	FMOD_RESULT res;
	if (sample == NULL)
		return false;
	if (sample->sample == NULL)
		return false;
	res = FMOD_System_PlaySound(
		system,
		FMOD_CHANNEL_FREE,
		sample->sample,
		true,
		&sample->channel);
	if (res != FMOD_OK)
		return false;
	FMOD_Channel_SetLoopCount(sample->channel, -1);
	FMOD_Channel_SetPaused(sample->channel, false);
	return true;
}

void Audio::Stop(string name)
{
	if (!IsPlaying(name))
		return;
	Sample* sample = FindSample(name);
	if (sample == NULL)
		return;
	FMOD_Channel_Stop(sample->channel);
}

void Audio::StopAll()
{
	for (Iterator i = samples.begin(); i != samples.end(); i++)
		FMOD_Channel_Stop((*i)->channel);
}

void Audio::StopAllExcept(string name)
{
	for (Iterator i = samples.begin(); i != samples.end(); i++)
		if ((*i)->getFileName() != name)
			FMOD_Channel_Stop((*i)->channel);
			
}